/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */

/*
 * NuevoModuloDialog.java
 *
 * Created on 18/06/2013, 06:48:39 PM
 */
package pe.edu.unmsm.cinfo.siga.ui.ventanas;

import java.beans.PropertyChangeListener;
import java.beans.PropertyChangeSupport;
import java.util.Collections;
import java.util.LinkedList;
import java.util.List;
import javax.swing.JOptionPane;
import javax.swing.event.ListSelectionEvent;
import javax.swing.event.ListSelectionListener;
import org.jdesktop.observablecollections.ObservableCollections;
import org.jdesktop.application.Action;
import pe.edu.unmsm.cinfo.siga.datos.CursoBase;
import pe.edu.unmsm.cinfo.siga.datos.Modulo;
import pe.edu.unmsm.cinfo.siga.datos.ModuloCurso;
import pe.edu.unmsm.cinfo.siga.datos.ModuloCursoPK;
import pe.edu.unmsm.cinfo.siga.ui.utils.ShortToBooleanConverter;
import pe.edu.unmsm.cinfo.siga.ui.utils.ValidadorModulo;

/**
 *
 * @author lartkma
 */
public class NuevoModuloDialog extends javax.swing.JDialog {
    
    /** Creates new form NuevoModuloDialog */
    public NuevoModuloDialog(java.awt.Frame parent, boolean modal) {
        super(parent, modal);
        initComponents();
        establecerOyentes();
    }
    
    private void establecerOyentes(){
        cursosDisponiblesList.addListSelectionListener(new ListSelectionListener() {
            @Override
            public void valueChanged(ListSelectionEvent e) {
                changeSupport.firePropertyChange("cursoDisponibleSeleccionado", 
                        !isCursoDisponibleSeleccionado(), 
                        isCursoDisponibleSeleccionado());
            }
        });
        cursosAsignadosTable.getSelectionModel().addListSelectionListener(new ListSelectionListener() {
            @Override
            public void valueChanged(ListSelectionEvent e) {
                changeSupport.firePropertyChange("cursoAsignadoSeleccionado", 
                        !isCursoAsignadoSeleccionado(), 
                        isCursoAsignadoSeleccionado());
            }
        });
    }
    
    public void abrir(Modulo moduloAEditar){
        setModuloEditado(moduloEditado);
        this.setVisible(true);
    }

    public Modulo getModuloEditado() {
        return moduloEditado;
    }

    public void setModuloEditado(Modulo moduloEditado) {
        assert moduloEditado != null;
        if(moduloEditado.getModuloCursoList() == null){
            moduloEditado.setModuloCursoList(new LinkedList<ModuloCurso>());
        }
        Modulo old = this.moduloEditado;
        this.moduloEditado = moduloEditado;
        changeSupport.firePropertyChange("moduloEditado", old, moduloEditado);
        setCursosModuloEditado(moduloEditado.getModuloCursoList());
        filtrarCursosDisponibles();
    }

    public List<ModuloCurso> getCursosModuloEditado() {
        return cursosModuloEditado;
    }
    
    private void setCursosModuloEditado(List<ModuloCurso> cursosModuloEditado) {
        List<ModuloCurso> old = this.cursosModuloEditado;
        this.cursosModuloEditado = ObservableCollections.observableList(cursosModuloEditado);
        changeSupport.firePropertyChange("cursosModuloEditado", old, this.cursosModuloEditado);
    }
    
    public void addPropertyChangeListener(PropertyChangeListener l){
        changeSupport.addPropertyChangeListener(l);
    }
    
    public void removePropertyChangeListener(PropertyChangeListener l){
        changeSupport.removePropertyChangeListener(l);
    }

    public boolean isGuardado() {
        return guardado;
    }

    public void setGuardado(boolean guardado) {
        this.guardado = guardado;
    }

    /** This method is called from within the constructor to
     * initialize the form.
     * WARNING: Do NOT modify this code. The content of this method is
     * always regenerated by the Form Editor.
     */
    @SuppressWarnings("unchecked")
    // <editor-fold defaultstate="collapsed" desc="Generated Code">//GEN-BEGIN:initComponents
    private void initComponents() {
        bindingGroup = new org.jdesktop.beansbinding.BindingGroup();

        entityManager = java.beans.Beans.isDesignTime() ? null : javax.persistence.Persistence.createEntityManagerFactory("AplicacionSIGAPU").createEntityManager();
        org.jdesktop.application.ResourceMap resourceMap = org.jdesktop.application.Application.getInstance(pe.edu.unmsm.cinfo.siga.ui.Main.class).getContext().getResourceMap(NuevoModuloDialog.class);
        consultaCursos = java.beans.Beans.isDesignTime() ? null : entityManager.createQuery(resourceMap.getString("consultaCursos.query")); // NOI18N
        listadoCursos = java.beans.Beans.isDesignTime() ? java.util.Collections.emptyList() : org.jdesktop.observablecollections.ObservableCollections.observableList(consultaCursos.getResultList());
        moduloEditado = null;
        cursosModuloEditado = null;
        desasignarButton = new javax.swing.JButton();
        asignarButton = new javax.swing.JButton();
        nombreTextField = new javax.swing.JTextField();
        nuevoCursoButton = new javax.swing.JButton();
        javax.swing.JLabel jLabel5 = new javax.swing.JLabel();
        estaActivoCheckbox = new javax.swing.JCheckBox();
        tieneRepitenciaCheckbox = new javax.swing.JCheckBox();
        descripcionTextField = new javax.swing.JTextField();
        javax.swing.JLabel jLabel1 = new javax.swing.JLabel();
        maximoCuotasSpinner = new javax.swing.JSpinner();
        minimoAlumnosSpinner = new javax.swing.JSpinner();
        javax.swing.JLabel jLabel4 = new javax.swing.JLabel();
        codigoTextField = new javax.swing.JTextField();
        javax.swing.JLabel jLabel2 = new javax.swing.JLabel();
        javax.swing.JLabel jLabel3 = new javax.swing.JLabel();
        jSeparator1 = new javax.swing.JSeparator();
        javax.swing.JLabel jLabel6 = new javax.swing.JLabel();
        javax.swing.JScrollPane jScrollPane1 = new javax.swing.JScrollPane();
        cursosDisponiblesList = new javax.swing.JList();
        jScrollPane2 = new javax.swing.JScrollPane();
        cursosAsignadosTable = new javax.swing.JTable();
        jButton1 = new javax.swing.JButton();
        jButton2 = new javax.swing.JButton();

        setDefaultCloseOperation(javax.swing.WindowConstants.DISPOSE_ON_CLOSE);
        setTitle(resourceMap.getString("Form.title")); // NOI18N
        setName("Form"); // NOI18N

        javax.swing.ActionMap actionMap = org.jdesktop.application.Application.getInstance(pe.edu.unmsm.cinfo.siga.ui.Main.class).getContext().getActionMap(NuevoModuloDialog.class, this);
        desasignarButton.setAction(actionMap.get("desasignarCurso")); // NOI18N
        desasignarButton.setName("desasignarButton"); // NOI18N

        asignarButton.setAction(actionMap.get("asignarCurso")); // NOI18N
        asignarButton.setName("asignarButton"); // NOI18N

        nombreTextField.setName("nombreTextField"); // NOI18N

        org.jdesktop.beansbinding.Binding binding = org.jdesktop.beansbinding.Bindings.createAutoBinding(org.jdesktop.beansbinding.AutoBinding.UpdateStrategy.READ_WRITE, this, org.jdesktop.beansbinding.ELProperty.create("${moduloEditado.modNom}"), nombreTextField, org.jdesktop.beansbinding.BeanProperty.create("text"));
        bindingGroup.addBinding(binding);

        nuevoCursoButton.setAction(actionMap.get("crearNuevoCurso")); // NOI18N
        nuevoCursoButton.setName("nuevoCursoButton"); // NOI18N

        jLabel5.setText("Nombre:"); // NOI18N
        jLabel5.setName("jLabel5"); // NOI18N

        estaActivoCheckbox.setText("Activo"); // NOI18N
        estaActivoCheckbox.setName("estaActivoCheckbox"); // NOI18N

        binding = org.jdesktop.beansbinding.Bindings.createAutoBinding(org.jdesktop.beansbinding.AutoBinding.UpdateStrategy.READ_WRITE, this, org.jdesktop.beansbinding.ELProperty.create("${moduloEditado.modEst}"), estaActivoCheckbox, org.jdesktop.beansbinding.BeanProperty.create("selected"));
        binding.setSourceNullValue(false);
        binding.setSourceUnreadableValue(false);
        binding.setConverter(converter);
        bindingGroup.addBinding(binding);

        tieneRepitenciaCheckbox.setText("Tiene factor de repitencia"); // NOI18N
        tieneRepitenciaCheckbox.setName("tieneRepitenciaCheckbox"); // NOI18N

        binding = org.jdesktop.beansbinding.Bindings.createAutoBinding(org.jdesktop.beansbinding.AutoBinding.UpdateStrategy.READ_WRITE, this, org.jdesktop.beansbinding.ELProperty.create("${moduloEditado.modRec}"), tieneRepitenciaCheckbox, org.jdesktop.beansbinding.BeanProperty.create("selected"));
        binding.setSourceNullValue(false);
        binding.setSourceUnreadableValue(false);
        binding.setConverter(converter);
        bindingGroup.addBinding(binding);

        descripcionTextField.setName("descripcionTextField"); // NOI18N

        binding = org.jdesktop.beansbinding.Bindings.createAutoBinding(org.jdesktop.beansbinding.AutoBinding.UpdateStrategy.READ_WRITE, this, org.jdesktop.beansbinding.ELProperty.create("${moduloEditado.modDesc}"), descripcionTextField, org.jdesktop.beansbinding.BeanProperty.create("text"));
        bindingGroup.addBinding(binding);

        jLabel1.setHorizontalAlignment(javax.swing.SwingConstants.RIGHT);
        jLabel1.setText("Codigo:"); // NOI18N
        jLabel1.setName("jLabel1"); // NOI18N

        maximoCuotasSpinner.setModel(new javax.swing.SpinnerNumberModel(Short.valueOf((short)0), Short.valueOf((short)0), null, Short.valueOf((short)1)));
        maximoCuotasSpinner.setName("maximoCuotasSpinner"); // NOI18N

        binding = org.jdesktop.beansbinding.Bindings.createAutoBinding(org.jdesktop.beansbinding.AutoBinding.UpdateStrategy.READ_WRITE, this, org.jdesktop.beansbinding.ELProperty.create("${moduloEditado.modMaxcuota}"), maximoCuotasSpinner, org.jdesktop.beansbinding.BeanProperty.create("value"));
        binding.setSourceNullValue(0);
        binding.setSourceUnreadableValue(0);
        bindingGroup.addBinding(binding);

        minimoAlumnosSpinner.setModel(new javax.swing.SpinnerNumberModel(Short.valueOf((short)0), Short.valueOf((short)0), null, Short.valueOf((short)1)));
        minimoAlumnosSpinner.setName("minimoAlumnosSpinner"); // NOI18N

        binding = org.jdesktop.beansbinding.Bindings.createAutoBinding(org.jdesktop.beansbinding.AutoBinding.UpdateStrategy.READ_WRITE, this, org.jdesktop.beansbinding.ELProperty.create("${moduloEditado.modNmin}"), minimoAlumnosSpinner, org.jdesktop.beansbinding.BeanProperty.create("value"));
        binding.setSourceNullValue(0);
        binding.setSourceUnreadableValue(0);
        bindingGroup.addBinding(binding);

        jLabel4.setHorizontalAlignment(javax.swing.SwingConstants.RIGHT);
        jLabel4.setText("Número cuotas:"); // NOI18N
        jLabel4.setName("jLabel4"); // NOI18N

        codigoTextField.setEditable(false);
        codigoTextField.setName("codigoTextField"); // NOI18N

        binding = org.jdesktop.beansbinding.Bindings.createAutoBinding(org.jdesktop.beansbinding.AutoBinding.UpdateStrategy.READ_WRITE, this, org.jdesktop.beansbinding.ELProperty.create("${moduloEditado.modCod}"), codigoTextField, org.jdesktop.beansbinding.BeanProperty.create("text"));
        bindingGroup.addBinding(binding);

        jLabel2.setHorizontalAlignment(javax.swing.SwingConstants.RIGHT);
        jLabel2.setText("Descripción:"); // NOI18N
        jLabel2.setName("jLabel2"); // NOI18N

        jLabel3.setHorizontalAlignment(javax.swing.SwingConstants.RIGHT);
        jLabel3.setText("Mínimo alumnos:"); // NOI18N
        jLabel3.setName("jLabel3"); // NOI18N

        jSeparator1.setName("jSeparator1"); // NOI18N

        jLabel6.setText("Cursos disponibles:"); // NOI18N
        jLabel6.setName("jLabel6"); // NOI18N

        jScrollPane1.setName("jScrollPane1"); // NOI18N

        cursosDisponiblesList.setName("cursosDisponiblesList"); // NOI18N

        org.jdesktop.swingbinding.JListBinding jListBinding = org.jdesktop.swingbinding.SwingBindings.createJListBinding(org.jdesktop.beansbinding.AutoBinding.UpdateStrategy.READ_WRITE, listadoCursos, cursosDisponiblesList);
        jListBinding.setDetailBinding(org.jdesktop.beansbinding.ELProperty.create("${curbNom}"));
        bindingGroup.addBinding(jListBinding);

        jScrollPane1.setViewportView(cursosDisponiblesList);

        jScrollPane2.setName("jScrollPane2"); // NOI18N

        cursosAsignadosTable.setName("cursosAsignadosTable"); // NOI18N
        cursosAsignadosTable.setSelectionMode(javax.swing.ListSelectionModel.SINGLE_SELECTION);
        cursosAsignadosTable.getTableHeader().setReorderingAllowed(false);

        org.jdesktop.beansbinding.ELProperty eLProperty = org.jdesktop.beansbinding.ELProperty.create("${cursosModuloEditado}");
        org.jdesktop.swingbinding.JTableBinding jTableBinding = org.jdesktop.swingbinding.SwingBindings.createJTableBinding(org.jdesktop.beansbinding.AutoBinding.UpdateStrategy.READ_WRITE, this, eLProperty, cursosAsignadosTable);
        org.jdesktop.swingbinding.JTableBinding.ColumnBinding columnBinding = jTableBinding.addColumnBinding(org.jdesktop.beansbinding.ELProperty.create("${modcOrd}"));
        columnBinding.setColumnName("Modc Ord");
        columnBinding.setColumnClass(Integer.class);
        columnBinding = jTableBinding.addColumnBinding(org.jdesktop.beansbinding.ELProperty.create("${cursoBase.curbNom}"));
        columnBinding.setColumnName("Curso Base.curb Nom");
        columnBinding.setColumnClass(String.class);
        columnBinding.setEditable(false);
        columnBinding = jTableBinding.addColumnBinding(org.jdesktop.beansbinding.ELProperty.create("${modcNhor}"));
        columnBinding.setColumnName("Modc Nhor");
        columnBinding.setColumnClass(Integer.class);
        jTableBinding.setSourceNullValue(Collections.EMPTY_LIST);
        jTableBinding.setSourceUnreadableValue(Collections.EMPTY_LIST);
        bindingGroup.addBinding(jTableBinding);
        jTableBinding.bind();
        jScrollPane2.setViewportView(cursosAsignadosTable);
        cursosAsignadosTable.getColumnModel().getColumn(0).setMaxWidth(50);
        cursosAsignadosTable.getColumnModel().getColumn(0).setHeaderValue(resourceMap.getString("cursosAsignadosTable.columnModel.title0")); // NOI18N
        cursosAsignadosTable.getColumnModel().getColumn(1).setHeaderValue(resourceMap.getString("cursosAsignadosTable.columnModel.title1")); // NOI18N
        cursosAsignadosTable.getColumnModel().getColumn(2).setMaxWidth(110);
        cursosAsignadosTable.getColumnModel().getColumn(2).setHeaderValue(resourceMap.getString("cursosAsignadosTable.columnModel.title2")); // NOI18N

        jButton1.setAction(actionMap.get("cancelar")); // NOI18N
        jButton1.setName("jButton1"); // NOI18N

        jButton2.setAction(actionMap.get("grabar")); // NOI18N
        jButton2.setName("jButton2"); // NOI18N

        javax.swing.GroupLayout layout = new javax.swing.GroupLayout(getContentPane());
        getContentPane().setLayout(layout);
        layout.setHorizontalGroup(
            layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addGroup(layout.createSequentialGroup()
                .addContainerGap()
                .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
                    .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
                        .addGroup(layout.createSequentialGroup()
                            .addComponent(jSeparator1, javax.swing.GroupLayout.DEFAULT_SIZE, 691, Short.MAX_VALUE)
                            .addContainerGap())
                        .addGroup(layout.createSequentialGroup()
                            .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.TRAILING, false)
                                .addComponent(jLabel4, javax.swing.GroupLayout.Alignment.LEADING, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, Short.MAX_VALUE)
                                .addComponent(jLabel3, javax.swing.GroupLayout.Alignment.LEADING, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, Short.MAX_VALUE))
                            .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
                            .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.TRAILING, false)
                                .addComponent(maximoCuotasSpinner)
                                .addComponent(minimoAlumnosSpinner, javax.swing.GroupLayout.DEFAULT_SIZE, 44, Short.MAX_VALUE))
                            .addGap(59, 59, 59)
                            .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
                                .addComponent(tieneRepitenciaCheckbox)
                                .addComponent(estaActivoCheckbox))
                            .addGap(367, 367, Short.MAX_VALUE))
                        .addGroup(layout.createSequentialGroup()
                            .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
                                .addGroup(layout.createSequentialGroup()
                                    .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.TRAILING)
                                        .addComponent(jLabel2, javax.swing.GroupLayout.DEFAULT_SIZE, 73, Short.MAX_VALUE)
                                        .addComponent(jLabel1, javax.swing.GroupLayout.DEFAULT_SIZE, 73, Short.MAX_VALUE))
                                    .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.UNRELATED)
                                    .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.TRAILING)
                                        .addGroup(layout.createSequentialGroup()
                                            .addComponent(codigoTextField, javax.swing.GroupLayout.PREFERRED_SIZE, 83, javax.swing.GroupLayout.PREFERRED_SIZE)
                                            .addGap(53, 53, 53)
                                            .addComponent(jLabel5)
                                            .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
                                            .addComponent(nombreTextField, javax.swing.GroupLayout.DEFAULT_SIZE, 427, Short.MAX_VALUE))
                                        .addComponent(descripcionTextField, javax.swing.GroupLayout.Alignment.LEADING, javax.swing.GroupLayout.DEFAULT_SIZE, 608, Short.MAX_VALUE)))
                                .addGroup(layout.createSequentialGroup()
                                    .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
                                        .addComponent(jLabel6)
                                        .addGroup(layout.createSequentialGroup()
                                            .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.CENTER)
                                                .addComponent(nuevoCursoButton)
                                                .addComponent(jScrollPane1, javax.swing.GroupLayout.PREFERRED_SIZE, 150, javax.swing.GroupLayout.PREFERRED_SIZE))
                                            .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.UNRELATED)
                                            .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING, false)
                                                .addComponent(asignarButton, javax.swing.GroupLayout.DEFAULT_SIZE, 58, Short.MAX_VALUE)
                                                .addComponent(desasignarButton, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, Short.MAX_VALUE))))
                                    .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.UNRELATED)
                                    .addComponent(jScrollPane2, javax.swing.GroupLayout.DEFAULT_SIZE, 463, Short.MAX_VALUE)))
                            .addContainerGap()))
                    .addGroup(javax.swing.GroupLayout.Alignment.TRAILING, layout.createSequentialGroup()
                        .addComponent(jButton2, javax.swing.GroupLayout.PREFERRED_SIZE, 102, javax.swing.GroupLayout.PREFERRED_SIZE)
                        .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.UNRELATED)
                        .addComponent(jButton1, javax.swing.GroupLayout.PREFERRED_SIZE, 102, javax.swing.GroupLayout.PREFERRED_SIZE)
                        .addContainerGap())))
        );
        layout.setVerticalGroup(
            layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addGroup(layout.createSequentialGroup()
                .addContainerGap()
                .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.BASELINE)
                    .addComponent(jLabel1)
                    .addComponent(codigoTextField, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE)
                    .addComponent(jLabel5)
                    .addComponent(nombreTextField, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE))
                .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
                .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.BASELINE)
                    .addComponent(jLabel2)
                    .addComponent(descripcionTextField, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE))
                .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
                .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.BASELINE)
                    .addComponent(jLabel3)
                    .addComponent(minimoAlumnosSpinner, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE)
                    .addComponent(tieneRepitenciaCheckbox))
                .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
                .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.BASELINE)
                    .addComponent(jLabel4)
                    .addComponent(maximoCuotasSpinner, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE)
                    .addComponent(estaActivoCheckbox))
                .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
                .addComponent(jSeparator1, javax.swing.GroupLayout.PREFERRED_SIZE, 10, javax.swing.GroupLayout.PREFERRED_SIZE)
                .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
                .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
                    .addGroup(layout.createSequentialGroup()
                        .addComponent(jLabel6)
                        .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
                            .addGroup(layout.createSequentialGroup()
                                .addGap(56, 56, 56)
                                .addComponent(asignarButton)
                                .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
                                .addComponent(desasignarButton))
                            .addGroup(layout.createSequentialGroup()
                                .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
                                .addComponent(jScrollPane1, javax.swing.GroupLayout.PREFERRED_SIZE, 221, javax.swing.GroupLayout.PREFERRED_SIZE)))
                        .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
                        .addComponent(nuevoCursoButton))
                    .addComponent(jScrollPane2, javax.swing.GroupLayout.PREFERRED_SIZE, 290, javax.swing.GroupLayout.PREFERRED_SIZE))
                .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
                .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.BASELINE)
                    .addComponent(jButton1, javax.swing.GroupLayout.PREFERRED_SIZE, 36, javax.swing.GroupLayout.PREFERRED_SIZE)
                    .addComponent(jButton2, javax.swing.GroupLayout.PREFERRED_SIZE, 36, javax.swing.GroupLayout.PREFERRED_SIZE))
                .addContainerGap(javax.swing.GroupLayout.DEFAULT_SIZE, Short.MAX_VALUE))
        );

        bindingGroup.bind();

        pack();
    }// </editor-fold>//GEN-END:initComponents

    @Action
    public void cancelar() {
        this.setGuardado(false);
        this.setVisible(false);
    }

    @Action
    public void grabar() {
        ValidadorModulo validador = new ValidadorModulo(moduloEditado);
        if(validador.esValido()){
            this.setGuardado(true);
            this.setVisible(false);
        }else{
            JOptionPane.showMessageDialog(this, validador.getMensaje(), "SIGA-CINFO", JOptionPane.INFORMATION_MESSAGE);
        }
    }

    @Action
    public void crearNuevoCurso() {
        NuevoCursoDialog dialogo = new NuevoCursoDialog(this, true);
        dialogo.setLocationRelativeTo(this);
        dialogo.setCurso(new CursoBase());
        dialogo.setVisible(true);
        if(dialogo.isGuardado()){
            entityManager.persist(dialogo.getCurso());
            entityManager.getTransaction().begin();
            entityManager.getTransaction().commit();
            listadoCursos.add(dialogo.getCurso());
            cursosDisponiblesList.ensureIndexIsVisible(listadoCursos.size());
        }
    }

    @Action(enabledProperty = "cursoDisponibleSeleccionado")
    public void asignarCurso() {
        int indice = cursosDisponiblesList.getSelectedIndex();
        CursoBase curso = listadoCursos.get(indice);
        
        List<ModuloCurso> listado = getCursosModuloEditado();
        
        ModuloCurso cursoEnModulo = new ModuloCurso();
        cursoEnModulo.setModuloCursoPK(new ModuloCursoPK());
        cursoEnModulo.getModuloCursoPK().setCurbCod(curso.getCurbCod());
        cursoEnModulo.setCursoBase(curso);
        cursoEnModulo.setModcOrd(listado.size()+1);
        cursoEnModulo.setModcNhor(0);
        listado.add(cursoEnModulo);
        
        listadoCursos.remove(curso);
    }

    @Action(enabledProperty = "cursoAsignadoSeleccionado")
    public void desasignarCurso(){
        int indice = cursosAsignadosTable.convertRowIndexToModel(cursosAsignadosTable.getSelectedRow());
        ModuloCurso cursoSeleccionado = getCursosModuloEditado().get(indice);
        getCursosModuloEditado().remove(cursoSeleccionado);        
        listadoCursos.add(cursoSeleccionado.getCursoBase());
    }
    
    private void filtrarCursosDisponibles(){
        if(moduloEditado == null) return;
        List<ModuloCurso> listado = getCursosModuloEditado();
        if(listado == null) return;
        for(ModuloCurso elemento : listado)
            listadoCursos.remove(elemento.getCursoBase());
    }

    public boolean isCursoDisponibleSeleccionado() {
        return cursosDisponiblesList.getSelectedIndex() != -1;
    }
    
    public boolean isCursoAsignadoSeleccionado(){
        return cursosAsignadosTable.getSelectedRow() != -1;
    }

    // Variables declaration - do not modify//GEN-BEGIN:variables
    private javax.swing.JButton asignarButton;
    private javax.swing.JTextField codigoTextField;
    private javax.persistence.Query consultaCursos;
    private javax.swing.JTable cursosAsignadosTable;
    private javax.swing.JList cursosDisponiblesList;
    private java.util.List<ModuloCurso> cursosModuloEditado;
    private javax.swing.JButton desasignarButton;
    private javax.swing.JTextField descripcionTextField;
    private javax.persistence.EntityManager entityManager;
    private javax.swing.JCheckBox estaActivoCheckbox;
    private javax.swing.JButton jButton1;
    private javax.swing.JButton jButton2;
    private javax.swing.JScrollPane jScrollPane2;
    private javax.swing.JSeparator jSeparator1;
    private java.util.List<CursoBase> listadoCursos;
    private javax.swing.JSpinner maximoCuotasSpinner;
    private javax.swing.JSpinner minimoAlumnosSpinner;
    private pe.edu.unmsm.cinfo.siga.datos.Modulo moduloEditado;
    private javax.swing.JTextField nombreTextField;
    private javax.swing.JButton nuevoCursoButton;
    private javax.swing.JCheckBox tieneRepitenciaCheckbox;
    private org.jdesktop.beansbinding.BindingGroup bindingGroup;
    // End of variables declaration//GEN-END:variables
    private PropertyChangeSupport changeSupport = new PropertyChangeSupport(this);
    private boolean guardado = false;
    private ShortToBooleanConverter converter = ShortToBooleanConverter.get();
}
